<?php
/**
 * Created by PhpStorm.
 * User: yu
 * Date: 2017/1/5
 * Time: 下午4:29
 */

namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller
{
	public function __construct ()
	{
		parent::__construct();
		if(method_exists($this,'__init')){
			$this->__init();
		}
	}

	public function store(Model $model,$data,\Closure $callback=null){
		$re = $model->store($data);
		if($re['status']=='success' && $callback instanceof \Closure){
			$callback($re);die;
		}
		$this->message($re);
	}
	public function message($re){
		if($re['status']=='success'){
			$this->success($re['message']);
		}else{
			$this->error($re['message']);
		}
		die;
	}

	/**
	 * uploadify上传方法
	 * 1.使用think的上传类将文件上传到服务器
	 * 2.将上传到服务器的文件路径返回给前台
	 */
	public function ajaxUpload(){
		$tpl = I('get.tpl',null);
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728000 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
		// 上传文件
		$info   =   $upload->upload();
		$info = current($info);
		$path[] = '/Uploads/'.$info['savepath'].$info['savename'];
//
		echo $this->getUploadTpl($path,$tpl);die;
	}

	public function getUploadTpl($path,$tpl=null){
		$this->assign('path',$path);
		return $this->fetch($tpl);
	}

	public function ajaxDel(){
		$path = I('post.path');
		$path = '.'.$path;
		if(is_file($path)){
			unlink($path);
		}
	}
}